package main;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.game.LayerManager;
import javax.microedition.lcdui.game.Sprite;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author WIN7
 */
public class MainMenuScreen extends GameCanvas implements Runnable{
    private MainMidlet midlet;
    private Image backgroundImage;
    private Sprite fire;
    private int[] seqFireFrame = {0, 1, 2, 3, 4, 5};
    private LayerManager layerManger;
    private boolean running;
    private int indexMenu;
    public MainMenuScreen(MainMidlet _midlet)
    {
        super(true);
        setFullScreenMode(true);
        midlet = _midlet;
        try
        {
            backgroundImage = Image.createImage("/BackgroundMainMenu.png");
            fire = new Sprite(Image.createImage("/FireAttack.png"), 136, 64);
        }catch(Exception ex)
        {
            ex.printStackTrace();
            return;
        }
        indexMenu = 0;
        fire.setFrameSequence(seqFireFrame);
        layerManger = new LayerManager();
        layerManger.append(fire);
        fire.setPosition(getWidth()/ 4, getHeight()/2);
    }
    
    public void start() {
        Thread t = new Thread(this);
        t.start();
        running = true;
    }
    
    public void stop()
    {
        running = false;
    }
    
    public void run()
    {
        Graphics g = getGraphics();
        while(running)
        {
            //
            updateStates();
            draw(g);
            try
            {
                Thread.sleep(200);
            }
            catch(Exception ex)
            {
                return;
            }
        }
    }
    
    private void updateStates()
    {
        int key = getKeyStates();
        if((key & LEFT_PRESSED) != 0)
        {
            indexMenu --;
            if(indexMenu < 0)
            {
                indexMenu = 3;
            }
        }else if((key & RIGHT_PRESSED) != 0)
        {
            indexMenu ++;
            indexMenu = indexMenu%4;
            
        }
        else if((key & FIRE_PRESSED) != 0)
        {
            switch(indexMenu)
            {
                case 0://story mode
                    stop();
                    midlet.showStoryModeMenu();
                    break;
                case 1://free battle
                    stop();
                    Pokemon bubasasPokemon = new Pokemon("Bubasas", "/Bubasas.png", 41, 41, 6, 4, 2, 2);
                    bubasasPokemon.getSprite().setPosition(0, 0);
                    midlet.showPlayingCanvas(new Character("/Character.png", 22, 27, bubasasPokemon), 
                            new Pokemon("Bubasas", "/Pidgey.png", 30, 30, 4, 4, 2, 4), new City("Battle city", 0, 0), true);
                    break;
                case 2://about
                    midlet.showAboutScreen();
                    break;
                case 3://exit
                    midlet.exit();
                    break;
                default:
                    break;
            }
        }
        fire.nextFrame();
    }
    
    private void draw(Graphics g)
    {
        g.drawImage(backgroundImage, 0, 0, 0);
        layerManger.paint(g, 0, 0);
        drawMenu(g);
        flushGraphics();
    }
    
    private void drawMenu(Graphics g)
    {
        String menuString = null;
        switch(indexMenu)
        {
            case 0:
                menuString = "<Story Mode>";
                break;
            case 1:
                menuString = "<Free Battle>";
                break;
            case 2:
                menuString = "<About>";
                break;
            case 3:
                menuString = "<Exit>";
                break;
            default:
                break;
        }
        g.setColor(255, 0, 0);
        g.drawString(menuString, getWidth()/2, getHeight()/ 2, Graphics.BASELINE | Graphics.HCENTER);
    }
}
